home *** CD-ROM | disk | FTP | other *** search
- /*
- SNEWS 2.0
-
- locking - a simple lock file manager
-
-
- Copyright (C) 1991 Malcolm S. Muir, Sunderland, UK.
- malcolm@muir.demon.co.uk
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License, version 1, as
- published by the Free Software Foundation.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- See the file COPYING, which contains a copy of the GNU General
- Public License.
- */
-
- /*---------------------------- Source Control ------------------------------*/
-
- /*
- * $Id: LOCKING.C,v 1.2 1994/02/05 18:48:42 gbj Exp user $
- */
-
- /****************************************************************************
- * 01 Jun 93 1.1 MSM Header added *
- * Snews 2.0 *
- * 09 Jul 93 1.2 MSM Correct error in lock file creation with short *
- * names. *
- ****************************************************************************/
-
- #include <stdio.h>
- #include <io.h>
- #include <fcntl.h>
- #include <string.h>
- #include "locking.h"
-
- #ifdef ATARI
- # include "fileops.h"
- #endif
-
- /*
- * create a lockfile
- */
-
- int mlock(char *dir, char *id, char *name)
- {
- int i;
- char lockname[65], tempname[65];
- int fd;
-
- strcpy(tempname, id);
- if (strlen(tempname) > 8) /* truncate long filenames */
- tempname[8] = '\0';
- if (tempname[7] == '\\')
- tempname[7] = '\0';
- for (i = 0; i < 7; i++)
- if (tempname[i] == '.') {
- tempname[i] = '\0';
- break;
- }
-
- /* Try to create the lock file in an atomic operation */
- sprintf(lockname, "%s%s.lck", dir, tempname);
- if ((fd = open(lockname, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1)
- return -1;
- write(fd, name, strlen(name));
- close(fd);
- return 0;
- }
-
- /*
- * remove a lockfile
- */
-
- int rmlock(char *dir, char *id, char *name)
- {
- int i;
- char lockname[65], tempname[65];
-
- name = name; /* to remove error message */
-
- strcpy(tempname, id);
- if (strlen(tempname) > 8) /* truncate long filenames */
- tempname[8] = '\0';
- if (tempname[7] == '/')
- tempname[7] = '\0';
- for (i = 0; i < 7; i++)
- if (tempname[i] == '.') {
- tempname[i] = '\0';
- break;
- }
- sprintf(lockname, "%s/%s.lck", dir, tempname);
- return (unlink(lockname));
- }
-